package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.Coachtransfer;
import com.gljx.web.Entity.Coachtransferentry;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.CoachtransferMapper;
import com.gljx.web.dao.CoachtransferentryMapper;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.service.CoachtransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/20 0020.
 */
@Transactional
@Service
public class CoachtransferServiceImpl extends CommonDao implements CoachtransferService {
    @Autowired
    CoachtransferMapper coachtransferMapper;
    @Autowired
    CoachtransferentryMapper coachtransferentryMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.CoachtransferMapper.pagecount", paramMap, null);
        return paramMap;
    }

    @Override
    public void addcoachtransfer(String state, Coachtransfer coachtransfer, String[] cfcoachid, String[] cfcoachno, String[] cfcarid, String[] cfcarno, String[] cfremark) {
        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Coachtransfer coachtransfertem = coachtransferMapper.selectByPrimaryKey(coachtransfer.getFid());
            coachtransfer.setFcreatorid(coachtransfertem.getFcreatorid());
            coachtransfer.setFcreatetime(coachtransfertem.getFcreatetime());
            coachtransferMapper.deleteByPrimaryKey(coachtransfer.getFid());
            coachtransferentryMapper.deleteByFparentid(coachtransfer.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(coachtransfer.getFfivouchered() + "")) {
                coachtransfer.setFauditorid(coachtransfertem.getFauditorid());
                coachtransfer.setCfbillstate(Long.valueOf("3"));
            }
            if ("4".equals(coachtransfer.getFfivouchered() + "")) {
                Axamine axamine = new Axamine();
                axamine.submissionProcess(String.valueOf(coachtransfer.getFnumber()));
            }
        }

        //*获取uuid*//*
        String uuid = GetFid.UUIDIntoFid();
        coachtransfer.setFid(uuid);
        if (cfcoachid != null || cfcoachid.length != 0) {
            //添加子类

            for (int i = 0; i < cfcoachid.length; i++) {
                Coachtransferentry coachtransferentry = new Coachtransferentry();

                coachtransferentry.setFid(GetFid.UUIDIntoFid());
                coachtransferentry.setFparentid(uuid);
                coachtransferentry.setCfcoachid(cfcoachid[i]);
                coachtransferentry.setCfcoachno(cfcoachno[i]);
                coachtransferentry.setCfcarid(cfcarid[i]);
                coachtransferentry.setCfcarno(cfcarno[i]);
                coachtransferentry.setCfremark(cfremark[i]);

                //如果传入状态为5则代表审核
                if ("5".equals(coachtransfer.getFfivouchered() + "")) {
                    //审核的操作
                }

                coachtransferentryMapper.insertSelective(coachtransferentry);
            }
            coachtransferMapper.insertSelective(coachtransfer);
        }
    }

    @Override
    public Coachtransfer selectCoachtransferByFid(String fid) {

        Coachtransfer coachtransfer = coachtransferMapper.selectByPrimaryKey(fid);
        List<Coachtransferentry> coachtransferentryList = coachtransferentryMapper.selectCoachtransferentryByFparentid(fid);
        if (coachtransfer != null) {
            coachtransfer.setCoachtransferentryList(coachtransferentryList);
        }


        return coachtransfer;
    }

    /**
     * @Author lcx
     * @Date 2017/12/14 0014 上午 11:04
     * 删除
     */
    @Override
    public void deleteByCoachtransferKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();

        for (int i = 0; i < deleteId.length; i++) {
            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            coachtransferMapper.deleteByPrimaryKey(deleteId[i]);
            coachtransferentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    @Override
    public ServerResponse examine(Coachtransfer coachtransfer, String state, String agreed, String auditcomment) {
        //如果是审核
        Axamine axamine = new Axamine();
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(coachtransfer.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == 1) {
                coachtransfer.setCfbillstate(Long.valueOf("5"));

                //获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                coachtransfer.setFlastupdatetime(createTime);
                //修改人id
                coachtransfer.setFauditorid(user.getFid());
                coachtransferMapper.updateByPrimaryKeySelective(coachtransfer);
            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                coachtransfer.setCfbillstate(Long.valueOf("3"));
                coachtransferMapper.updateByPrimaryKeySelective(coachtransfer);
            }
            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            coachtransfer.setCfbillstate(Long.valueOf("3"));
            coachtransfer.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(coachtransfer.getFnumber(), auditcomment);
            coachtransferMapper.updateByPrimaryKeySelective(coachtransfer);
            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    @Override
    public List<Coachtransfer> selectCoachtransferAsXls(String jlName, String cfcoachno, String beginTime, String endTime, String fcompanyid) {
        return coachtransferMapper.selectCoachtransferAsXls(jlName, cfcoachno, beginTime, endTime, fcompanyid);
    }

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.CoachtransferMapper.pagemap", paramMap, null);
        return paramMap;
    }
}
